package com.xiaotu.spider.youku.pageprocessor;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 YouKuHotPageProcessor
 * @日期 2017年2月13日
 * @作者 高海军
 * @功能 优酷网站热剧爬取
 */
@Component("YouKuHotPageProcessor")
@Scope("prototype")
public class YouKuHotPageProcessor extends AUrlPageProcessor
{
	@Override
	protected String[] getStartRequestUrls()
	{
		return new String[] {
				"http://list.youku.com/category/show/c_97_a_%E5%A4%A7%E9%99%86_s_1_d_1.html?spm=a2h1n.8251845.selectID.5!2~5~5!2~1~3~A", // 内地当日最多播放
				"http://list.youku.com/category/show/c_97_a_%E5%A4%A7%E9%99%86_s_6_d_1.html?spm=a2h1n.8251845.selectID.5!2~5~5!2~1~3!5~A",// 内地最近更新
				"http://list.youku.com/category/show/c_97_a_%E9%A6%99%E6%B8%AF_s_1_d_1.html?spm=a2h1n.8251845.filterPanel.5!2~1~3!3~A",//香港当日最多播放
				"http://list.youku.com/category/show/c_97_a_%E9%A6%99%E6%B8%AF_s_6_d_1.html?spm=a2h1n.8251845.selectID.5!2~5~5!2~1~3!5~A",//香港最近更新
				"http://list.youku.com/category/show/c_97_a_%E5%8F%B0%E6%B9%BE_s_1_d_1.html?spm=a2h1n.8251845.filterPanel.5!2~1~3!4~A",//台湾当日最多播放
				"http://list.youku.com/category/show/c_97_a_%E5%8F%B0%E6%B9%BE_s_6_d_1.html?spm=a2h1n.8251845.selectID.5!2~5~5!2~1~3!5~A"//台湾最近更新
		};
	}
	
	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.YOUKU_HOT;
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return Constants.PageDataType.SEARCH_LIST;
	}
	
	@Override
	public void process(Page page)
	{
		List<String> divList = page.getHtml()
				.xpath("//div[@class='box-series']/ul[@class='panel']/li")
				.all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		List<HotTVModel> tvList = new ArrayList<HotTVModel>();
		for (String divStr : divList)
		{
			Html divObj = new Html(divStr);
			tvList.add(new HotTVModel(Constants.SiteName.YOUKU,
					divObj.xpath("//div[@class='p-thumb']/a/@title").get(),
					divObj.xpath(
							"//ul[@class='info-list']/li[@class='actor']/a/@title")
							.all(),
					divObj.xpath("//div[@class='p-thumb']/a/@href").get()));
		}
		page.putField(HotTVModel.class.getName(), tvList);
		
		String current = page.getHtml()
				.xpath("//ul[@class='yk-pages']/li[@class='current']/span/text()")
				.get();
		if (StringUtils.isNotEmpty(current))
		{
			current = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, current);
			if (StringUtils.isNotEmpty(current))
			{
				if (PropertiesUtil.getPropertyByInt(
						Constants.SpiderConfigKey.HOTTV_PAGE_SIZE) > Integer
								.parseInt(current))
					PageUtils.addTargetRequest(page,
							page.getHtml()
									.xpath("//ul[@class='yk-pages']/li[@class='next']/a/@href")
									.get(),
							page.getRequest().getExtras());
			}
		}
	}
}
